package com.dd.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.*;

public class DdDeliveryDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdDeliveryDAO.class);

	public void saveOrUpdateDdDelivery(DdDelivery transientInstance) {
		log.debug("saving DdDelivery instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void saveOrUpdateMenu(DdDeliveryMenu transientInstance) {
		log.debug("saving DdDeliveryMenu instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	// 删送餐商家,同时删了关联菜单
	public void delDdDelivery(DdDelivery persistentInstance) {
		log.debug("deleting DdDelivery instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			List list = this.findMenuByID(persistentInstance.getId());
			getHibernateTemplate().deleteAll(list);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	// 删送餐商家,同时删了关联菜单
	public void delDdDeliveryMenuBYDdID(int id) {
		try {
			List list = this.findMenuByID(id);
			getHibernateTemplate().deleteAll(list);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdDelivery findDdDeliveryByName(String name) {
		log.debug("findDdDeliveryByName: " + name);
		try {
			List list = getHibernateTemplate().find(
					"from DdDelivery where name='" + name+"'");
			if (list.size() > 0) {
				DdDelivery instance = (DdDelivery) list.get(0);
				return instance;
			} else {
				return null;
			}

		} catch (RuntimeException re) {
			log.error("get failed", re);
			return null;
		}
	}

	public DdDelivery findDdDeliveryById(java.lang.Integer id) {
		log.debug("findById DdDelivery with id: " + id);
		try {
			DdDelivery instance = (DdDelivery) getHibernateTemplate().get(
					"com.dd.entity.DdDelivery", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	// 根据商家id找菜单
	public List findMenuByID(java.lang.Integer id) {
		log.debug("findMenuByID ");
		try {
			return getHibernateTemplate().find(
					"from DdDeliveryMenu where deliveryid=" + id);
		} catch (RuntimeException re) {
			log.error("findMenuByID failed", re);
			throw re;
		}
	}

	// 所有商家
	public List findAllDelvery() {
		log.debug("findMenuByID ");
		try {
			return getHibernateTemplate().find("from DdDelivery");
		} catch (RuntimeException re) {
			log.error("findMenuByID failed", re);
			throw re;
		}
	}

	// 根据坐标象限找所有商家
	public List findAllDelvery(String posx1, String posy1, String posx2,
			String posy2) {
		log.debug("findMenuByID ");
		try {
			return getHibernateTemplate().find(
					"from DdDelivery where posx>'" + posx1 + "' and posx<'"
							+ posx2 + "' and  posy>'" + posy1 + "' and posy<'"
							+ posy2 + "'");
		} catch (RuntimeException re) {
			log.error("findMenuByID failed", re);
			throw re;
		}
	}
}
